package com.buddy.demo.spring.schedule;

import com.buddy.demo.spring.event.SpringEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Random;

@Component
public class SpringEventPublishSchedule implements ApplicationEventPublisherAware {

    private ApplicationEventPublisher publisher;

    @Scheduled(fixedDelay = 10_000L)
    public void publishEvent(){
        publisher.publishEvent(new SpringEvent(this,System.currentTimeMillis()+"", new Random(10).nextInt()));
        System.out.println("===========================================================");
    }

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.publisher = applicationEventPublisher;
    }
}
